package com.stox.navigator.ui.swing.event;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.SwingUtilities;

import com.stox.charting.ui.swing.action.ReplaceChartAction;
import com.stox.core.ui.swing.util.SwingUtil;
import com.stox.design.Callback;
import com.stox.ioc.Container;
import com.stox.navigator.ui.swing.widget.NavigatorPopupMenu;

public class NavigatorTreeMouseAdapter extends MouseAdapter {

	private ReplaceChartAction replaceChartAction;
	private NavigatorPopupMenu navigatorPopupMenu;
	
	public NavigatorTreeMouseAdapter() {
		this.replaceChartAction = Container.get(ReplaceChartAction.class);
		this.navigatorPopupMenu = Container.get(NavigatorPopupMenu.class);
	}
	
	@Override
	public void mouseReleased(final MouseEvent e) {
		SwingUtil.executeInFreeze((Component)e.getSource(), new Callback<Object, Object>(){
			@Override
			public Object execute(Object payload) {
				if(SwingUtilities.isRightMouseButton(e)){
					navigatorPopupMenu.show((Component)e.getSource(), e.getX(), e.getY());
				}else if(SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2){
					ActionEvent event = new ActionEvent(e.getSource(), e.getID(), "");
					replaceChartAction.actionPerformed(event);
				}
				return null;
			}
		}, null);
	}
}
